package com.zglc.ms.shop.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Maps;
import com.zglc.ms.base.constants.ResponseCodeHelper;
import com.zglc.ms.shop.controller.log.RequestLog;
import com.zglc.ms.shop.filter.JwtHelper;
import com.zglc.ms.shop.model.UserPoint;
import com.zglc.ms.shop.model.UserPointRecord;
import com.zglc.ms.shop.service.PointService;
import com.zglc.ms.shop.util.CommonUtils;

@Controller
@RequestMapping("/front/point")
public class PointController {

	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	PointService pointService;

	@RequestMapping("/userPoint")
	@ResponseBody
	@RequestLog
	public Map<String, Object> userPoint(HttpServletRequest request){
		Map<String, Object> map = CommonUtils.successResponse();
		Map<String, Object> resultMap = Maps.newHashMap();
		resultMap.put("available_point", 0);
		resultMap.put("total_point", 0);
		Integer userId = JwtHelper.getUserId(request);
		if(userId == null){
			return CommonUtils.userNotExistResponse();
		}
		List<UserPoint> userPointList = pointService.getUserPoint(userId);

		if(!CollectionUtils.isEmpty(userPointList) && userPointList.size()>0){
			for(UserPoint userPoint : userPointList){
				resultMap.put("available_point_"+userPoint.getType(), userPoint.getAvailablePoint());
				resultMap.put("total_point_"+userPoint.getType(), userPoint.getTotalPoint());
			}
		}
		
		map.put("data", resultMap);
		logger.info(map.toString());
		return map;
		
	}
	
	@RequestMapping("/getRecord")
	@ResponseBody
	@RequestLog
	public Map<String, Object> getRecord(HttpServletRequest request, @RequestParam int type,  @RequestParam int page, @RequestParam int pageNum){
		Map<String, Object> map = CommonUtils.successResponse();
		Integer userId = JwtHelper.getUserId(request);
		if(userId == null){
			return CommonUtils.userNotExistResponse();
		}
		
		logger.info("type " + type + " userId " + userId);
		
		if(type >1 || type <0){
			map.put("code", ResponseCodeHelper.COMMON_PARAMS_INVALID);
			map.put("msg", "请求参数有误，请检查！");
		}
		
		List<UserPointRecord> userPointRecord = pointService.getUserPointRecord(userId, type, page, pageNum);
		
		Integer userPointRecordCnt = pointService.getUserPointRecordCnt(userId, type);
		
		if(userPointRecord == null){
			map.put("data", new ArrayList<>());
			map.put("cnt", 0);
			logger.info(map.toString());
			return map;
		}
		
		map.put("data", userPointRecord);
		map.put("cnt", userPointRecordCnt == null ? 0 : userPointRecordCnt);
		logger.info(map.toString());
		return map;
		
	}
	

	
}